import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TaskRemoveTest {
    /**
     * 使用remove方法
     * 已经正在运行中的任务不可以删除
     * execute方法提交的，未运行的任务可以删除
     * submit方法提交的，未运行的任务不可以删除，小心踩坑
     */
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,100, TimeUnit.SECONDS
        ,new LinkedBlockingDeque<>());
        Runnable run = new RunnerDemo("A");
        Runnable run2 = new RunnerDemo("B");
        executor.execute(run);

        executor.execute(run2);
        Thread.sleep(1000);
        System.out.println("线程池中的任务数 = " + executor.getTaskCount());
        boolean remove = executor.remove(run);
        System.out.println("remove = " + remove);
        System.out.println("线程池中的任务数 = " + executor.getTaskCount());
        boolean remove1 = executor.remove(run2);
        System.out.println("remove = " + remove1);
        System.out.println("线程池中的任务数 = " + executor.getTaskCount());
    }
}
